package com.yswl.pri.picture;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.SlidingTabLayout;
import com.yswl.pri.R;
import com.yswl.pri.common.ToolbarMFragment;
import com.yswl.pri.picture.entry.ImageSourceCategory;
import com.yswl.pri.picture.entry.ImageSourceDBCategory;
import com.yswl.pri.picture.entry.ImageSourceLoftCategory;

import java.util.ArrayList;
import java.util.List;

import yswl.com.klibrary.base.MFragment;
import yswl.com.klibrary.http.okhttp.MScreenUtils;
import yswl.com.klibrary.util.L;

public class PictureTabLayoutFragment extends ToolbarMFragment {

    private static final String TAG = PictureTabLayoutFragment.class.getSimpleName();

    public static PictureTabLayoutFragment newInstance() {
        PictureTabLayoutFragment fragment = new PictureTabLayoutFragment();
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_picture_tab_layout, container, false);
    }

    private SlidingTabLayout mTabLayout;
    private ViewPager mViewPager;
    private final String[] mTitles = {
            ImageSourceLoftCategory.WY.getName(),
//            "百度",
            ImageSourceCategory.FULI.getName(),
            ImageSourceDBCategory.FIVE.getName(),
            ImageSourceDBCategory.FOUR.getName(),
            ImageSourceDBCategory.THREE.getName(),
            ImageSourceDBCategory.SIX.getName(),
//            ImageSourceCategory.MM.getName(),
//            ImageSourceCategory.MZ.getName(),
//            ImageSourceCategory.JD.getName()
    };

    @Override
    protected String getToolbarTitle() {
        return "美图";
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        mTabLayout = view.findViewById(R.id.tablayout);

        L.e(TAG, "tab_width :" + mTabLayout.getTabWidth());
        mViewPager = view.findViewById(R.id.fragment_list);
        initViewPager();
    }

    private void initViewPager() {
        // 创建一个集合,装填Fragment
        List<MFragment> fragments = new ArrayList<>();
        fragments.add(PictureListLoftFragment.newInstance(ImageSourceLoftCategory.WY));
//        fragments.add(new PictureListFragment());
        fragments.add(PictureList2Fragment.newInstance(ImageSourceCategory.FULI));
        fragments.add(PictureListDBFragment.newInstance(ImageSourceDBCategory.FIVE));
        fragments.add(PictureListDBFragment.newInstance(ImageSourceDBCategory.FOUR));
        fragments.add(PictureListDBFragment.newInstance(ImageSourceDBCategory.THREE));
        fragments.add(PictureListDBFragment.newInstance(ImageSourceDBCategory.SIX));
//        fragments.add(PictureList3Fragment.newInstance(ImageSourceCategory.MM));
//        fragments.add(PictureList3Fragment.newInstance(ImageSourceCategory.MZ));
//        fragments.add(PictureList3Fragment.newInstance(ImageSourceCategory.JD));
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getChildFragmentManager());
        myPagerAdapter.setFragments(fragments);
        // 给ViewPager设置适配器
        mViewPager.setAdapter(myPagerAdapter);
        mTabLayout.setViewPager(mViewPager);
        mViewPager.setCurrentItem(0);
        setCusTabWidth();
    }

    void setCusTabWidth() {
        int tabCount = mTabLayout.getTabCount();
        float tabWidth = mTabLayout.getTabWidth();
        int screenWidth = MScreenUtils.getScreenWidth(getMActivity());

        if (tabCount * tabWidth < screenWidth) {
            float needWidth = MScreenUtils.getScreenWidth(getMActivity()) / mTitles.length;
            int dpNeedWidth = MScreenUtils.px2dip(getMActivity(), needWidth);
            mTabLayout.setTabWidth(dpNeedWidth);
        }
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        private List<MFragment> mFragmentList;

        public void setFragments(List<MFragment> fragments) {
            mFragmentList = fragments;
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public MFragment getItem(int position) {
            MFragment fragment = mFragmentList.get(position);
            return fragment;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }
    }
}
